//
//  KLMBaseRowItemCell.swift
//  KLM_GN
//
//  Created by Nic on 2025/2/27.
//

import UIKit

protocol KLMBaseRowItemCellDelegate: AnyObject {
    func rowItemCellToggleOn(cell: KLMBaseRowItemCell, index: IndexPath, on tableView: UITableView)
    func rowItemCellTapArrowBtn(cell: KLMBaseRowItemCell, index: IndexPath, on tableView: UITableView)
    func rowItemCellTapRightBtn(cell: KLMBaseRowItemCell, index: IndexPath, on tableView: UITableView)
}

extension KLMBaseRowItemCellDelegate {
    func rowItemCellToggleOn(cell: KLMBaseRowItemCell, index: IndexPath, on tableView: UITableView) {}
    func rowItemCellTapArrowBtn(cell: KLMBaseRowItemCell, index: IndexPath, on tableView: UITableView) {}
    func rowItemCellTapRightBtn(cell: KLMBaseRowItemCell, index: IndexPath, on tableView: UITableView) {}
}

class KLMBaseRowItemCell: UITableViewCell {

    let line: UIView = {
        let view = UIView()
        view.backgroundColor = rgba(222, 222, 222, 1)
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    
    @IBOutlet weak var titleLab: UILabel!
    @IBOutlet weak var subTitleLab: UILabel!
    @IBOutlet weak var rightBtn: UIButton!
    @IBOutlet weak var onOffBtn: UIButton!
    @IBOutlet weak var arrowBtn: UIButton!
    @IBOutlet weak var titleAlignCenterCons: NSLayoutConstraint!
    @IBOutlet weak var titleTopCons: NSLayoutConstraint!
    @IBOutlet weak var rightCons: NSLayoutConstraint!
    @IBOutlet weak var onOffRightCons: NSLayoutConstraint!
    
    var index: IndexPath = []
    var tableView: UITableView?
    
    weak var delegate: KLMBaseRowItemCellDelegate?
    
    var showLine = false {
        didSet {
            line.isHidden = !showLine
        }
    }
    
    var item: KLMRowItem! {
        didSet {
            titleLab.configStyle(with: item.title, titleColor: item.titleColor)
            
            titleAlignCenterCons.priority = .defaultHigh
            titleTopCons.priority = .defaultLow
            subTitleLab.text = ""
            if let subTitle = item.subTitle, !subTitle.isEmpty {
                titleAlignCenterCons.priority = .defaultLow
                titleTopCons.priority = .defaultHigh
                subTitleLab.text = subTitle
            }
            
            rightBtn.isHidden = true
            if let rightTitle = item.rightTitle, !rightTitle.isEmpty {
                rightBtn.configStyle(with: rightTitle, titleColor: item.rightTitleColor)
                rightBtn.isHidden = false
            }
            
            if let rightImg = item.rightImage, !rightImg.isEmpty {
                rightBtn.setImage(UIImage(named: rightImg)?.croppedImage(with: 7, imgSize: CGSize(width: 32, height: 32), borderWidth: 1.0, borderColor: rgba(0, 0, 0, 0.06)), for: .normal)
                rightBtn.isHidden = false
            }
            if item.rightBorder {
                rightBtn.contentEdgeInsets = UIEdgeInsets(top: 2, left: 8, bottom: 2, right: 8)
                if item.rightImage != nil && !item.rightImage!.isEmpty {
                    rightBtn.contentEdgeInsets = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2)
                }
                rightBtn.layer.borderWidth = 1.0
                rightBtn.layer.borderColor = item.rightTitleColor.cgColor
                rightBtn.layer.cornerRadius = 6.0
                rightBtn.layer.masksToBounds = true
            } else {
                rightBtn.contentEdgeInsets = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2)
                rightBtn.layer.borderWidth = 0.0
            }
            
            if item.showSwitch {
                onOffBtn.setImage(UIImage(named: item.offImage), for: .normal)
                onOffBtn.setImage(UIImage(named: item.onImage), for: .selected)
            }
            onOffBtn.isHidden = !item.showSwitch
            onOffBtn.isSelected = item.isOn
            
            rightCons.constant = 16.0
            onOffRightCons.constant = 16.0
            if item.showArrow {
                rightCons.constant = 36.0
                onOffRightCons.constant = 36.0
            }
            
            if item.showArrow {
                arrowBtn.setImage(UIImage(named: item.arrowIcon), for: .normal)
            }
            arrowBtn.isHidden = !item.showArrow
        }
    }
    
    // 静态方法，传入 tableView 和 indexPath，返回该 cell
    static func cell(with tableView: UITableView, at indexPath: IndexPath, cellId: String) -> Self {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! Self
        cell.index = indexPath
        cell.tableView = tableView
        return cell
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        titleLab.configStyle(font: UIFont(safeName: "PingFangSC-Regular", size: 16), titleColor: rgba(38, 38, 38, 1))
        subTitleLab.configStyle(font: UIFont(safeName: "PingFangSC-Regular", size: 12), titleColor: rgba(129, 129, 129, 1))
        rightBtn.configStyle(font: UIFont(safeName: "PingFangSC-Regular", size: 12), titleColor: rgba(129, 129, 129, 1))
        
        addSubview(line)
        line.snp.makeConstraints { make in
            make.leading.equalTo(16)
            make.trailing.equalTo(-16)
            make.top.equalToSuperview()
            make.height.equalTo(0.5)
        }
        line.isHidden = true
    }

    func setFont(_ font: UIFont?) {
        titleLab.font = font
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    @IBAction func tapOnOff(_ sender: UIButton) {
        sender.isSelected = !sender.isEnabled
        if let tableView = tableView {
            delegate?.rowItemCellToggleOn(cell: self, index: index, on: tableView)
        }
    }
    
    @IBAction func tapRightBtn(_ sender: UIButton) {
        if let tableView = tableView {
            delegate?.rowItemCellTapRightBtn(cell: self, index: index, on: tableView)
        }
    }
    @IBAction func tapArrow(_ sender: UIButton) {
        sender.isSelected = !sender.isEnabled
        if let tableView = tableView {
            delegate?.rowItemCellTapArrowBtn(cell: self, index: index, on: tableView)
        }
    }
}
